package com.piao.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;
import com.piao.R;
import com.piao.adapter.HomeMovieAdapter;
import com.piao.bean.JsonRootBean;
import com.piao.bean.Movies;
import com.piao.utils.Urls;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;



/**
 * 开发中
 */
public class MovieFragment extends Fragment implements View.OnClickListener{


    private static final String TAG ="MovieFragment" ;
    private RecyclerView recyclerView;
    private HomeMovieAdapter adapter;

    public static MovieFragment newInstance() {
        return new MovieFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_main_develop, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        recyclerView= (RecyclerView) view.findViewById(R.id.recyclerview);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(getContext());
        mLinearLayoutManager.setOrientation(GridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(mLinearLayoutManager);
        getMovies();
    }

    @Override
    public void onClick(View v) {
    }

    private void getMovies(){
        OkGo.get(Urls.nowMovies)     // 请求方式和请求 url
                .tag(this)                       // 请求的 tag, 主要用于取消对应的请求
                .cacheKey(TAG)            // 设置当前请求的缓存 key,建议每个不同功能的请求设置一个
                .cacheMode(CacheMode.DEFAULT)    // 缓存模式，详细请看缓存介绍
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        Gson gson=new Gson();
                        JsonRootBean bean=gson.fromJson(s, JsonRootBean.class);
                        List<Movies> list = bean.movies;
                        adapter=new HomeMovieAdapter(getContext(),list);
                        recyclerView.setAdapter(adapter);

                    }
                });
    }
}
